package com.tvsinfo.jdk.demo.case4_stream;

import com.tvsinfo.jdk.demo.case2_function_interface.dto.Car;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ToList、toMap
 */
public class Stream1_collection implements Stream_Create_List {

    public static void main(String[] args) {
        Stream1_collection streamCollectionDemo = new Stream1_collection();
        List<Integer> list = Arrays.asList(1, 2, 3, 5, 6);

        list.stream().peek(integer -> {

            System.out.println(integer + "integer");
        }).forEach(integer -> {
        });


    }


    /**
     * 一、中间操作
     */
    public void centerOpt() {
        List<Car> cars = getList();

        //map
        System.out.println("-----------------------------------【map转换】");
        List<String> list1 = cars.stream().map(Car::getBrand).collect(Collectors.toList());
        list1.stream().peek(s -> {
            System.out.println(s + "-----------------");
        }).forEach(System.out::println);


        //filter
        System.out.println("-----------------------------------【filter过滤】");
        List list2 = cars.stream()
                .filter(car -> car.getColor().equals("白色") && car.getPrice() >= 50 && car.getPrice() <= 60)
                .peek(car -> {
                    System.out.print("+++++++++" + car);
                })
                .filter(car -> car.getBrand().contains("奔驰"))
                .peek(car -> {
                    System.out.print("---------" + car);
                })
                .collect(Collectors.toList());
        list2.stream().forEach(System.out::println);

        //distinct
        System.out.println("-----------------------------------【颜色去重复】");
        List<String> list3 = cars.stream().map(Car::getColor).distinct().collect(Collectors.toList());
        list3.stream().forEach(System.out::println);

        //sorted
        System.out.println("-----------------------------------【价格排序】");
        List<Car> list4 = cars.stream().sorted((o1, o2) -> o1.getPrice().compareTo(o2.getPrice())).collect(Collectors.toList());
        list4.stream().forEach(System.out::print);

        //(工具处理)
        cars.stream().sorted(Comparator.comparing(Car::getPrice)).collect(Collectors.toList());

        //limit
        System.out.println("-----------------------------------【价格前3】");
        List<Car> list5 = cars.stream().sorted((o1, o2) -> o2.getPrice().compareTo(o1.getPrice())).limit(3).collect(Collectors.toList());
        list5.stream().forEach(System.out::print);
    }


    /**
     * 二、集合器-转成集合-终端操作
     */
    public void collectionTL() {
        List<Car> cars = getList();


        //toList
        System.out.println("-----------------------------------【1.toList】");
        List<List<String>> list = cars.stream().map(car -> car.getAgency()).collect(Collectors.toList());
        list.parallelStream().forEach(System.out::print);
        System.out.println();


        //toMap(无重复)merge、forEach、putIfAbsent()
        System.out.println("-----------------------------------【2.toMap】");
        Map<String, Car> carMap = cars.stream().distinct().collect(Collectors.toMap((Car::getBrand), (aq -> aq)));
        carMap.forEach((s, car) -> {
            System.out.println("key:" + s + " value:" + car);
        });
        System.out.println();


        //有重复(案例1)
        System.out.println("-----------------------------------【3.有重复】1");
        Map<Integer, List<Car>> integerListMap22 = cars.stream().distinct().collect(Collectors.toMap((Car::getPrice), (aq -> Arrays.asList(aq))));
        System.out.println();
        System.out.println();
        System.out.println();


        //有重复(案例1)
        System.out.println("-----------------------------------【4.有重复】2");
        Map<Integer, List<Car>> integerListMap = cars.stream().distinct().collect(Collectors.toMap((Car::getPrice), (aq -> Arrays.asList(aq)), (o, o2) -> {
            o.addAll(o2);
            return o;
        }));


        //有重复(案例2)
        Map<Integer, List<Car>> integerListMap_1 = cars.stream().distinct().collect(Collectors.toMap((Car::getPrice), Stream1_collection::asList, (o, o2) -> {
            o.addAll(o2);
            return o;
        }));

        integerListMap_1.forEach((s, carList) -> {
            System.out.println("key:" + s + " value:");
            carList.forEach(System.out::print);
            System.out.println("-----------------------------");
        });


        //有重复
        System.out.println("-----------------------------------【5.有重复、Map类型】");
        Map<Integer, List<Car>> integerListMap2 = cars.stream().distinct().collect(Collectors.toMap((Car::getPrice), Stream1_collection::asList, (o, o2) -> {
            o.addAll(o2);
            return o;
        }, TreeMap::new));

        integerListMap2.forEach((s, carList) -> {
            System.out.println("key:" + s + " value:");
            carList.forEach(System.out::print);
        });
        Map<Long, BigDecimal> ss = new HashMap<>();
        ss.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).findFirst();

//        ss.forEach((aLong, bigDecimal) ->);

    }


    public static List<Car> asList(Object o) {
        List list = new ArrayList();
        list.add(o);
        return list;
    }

}
